package com.chezhujie.www.core.quartz;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;

import net.sf.ehcache.search.expression.Or;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.chezhujie.www.domain.Czjorder;
import com.chezhujie.www.domain.vo.OrderDateVO;
import com.chezhujie.www.domain.vo.OrderIdAndDate;
import com.chezhujie.www.enums.OrderStatus;
import com.chezhujie.www.service.CzjorderService;
import com.chezhujie.www.utils.LocalDateTime;

/**
 * 会话
 * 
 * @author 汤伟杰
 *
 */
@Controller
public class AppMap {

	private static Map<String, Map<String, Object>> vcode;// 验证码缓存

	private static Map<String, Long> businOrder;// 商品订单缓存
	
	private static Map<String, Map<String, Object>> randomStr;//验证码随机数缓存

	private static boolean one;

	private static AppMap factory;

	@Autowired
	private CzjorderService czjorderService;

	@PostConstruct
	public void init() {
		factory = this;
	}

	public static Map<String, Long> getBusinOrder() {
		if (businOrder == null) {
			businOrder = new HashMap<String, Long>();
		}
		return businOrder;
	}

	public static void setBusinOrder(String orderId) {

		if (StringUtils.isNotBlank(orderId)) {
			if (businOrder == null) {
				businOrder = new HashMap<String, Long>();
			}
			businOrder.put(orderId, System.currentTimeMillis());
		}

	}

	/**
	 * 设置验证码缓存
	 * 
	 * @param vali
	 * @param tk
	 */
	public static void setVcode(String vali, String tk) {
		if (StringUtils.isNotBlank(tk)) {
			if (vcode == null) {
				vcode = new HashMap<String, Map<String, Object>>();
			}
			if (vcode.get(tk) != null) {
				vcode.remove(tk);
			}
			Map<String, Object> object = new HashMap<String, Object>();
			object.put("name", vali);
			object.put("creattime", System.currentTimeMillis());
			vcode.put(tk, object);
		}

	}

	public static Map<String, Map<String, Object>> getVcode() {
		if (vcode == null) {
			vcode = new HashMap<String, Map<String, Object>>();
		}
		return vcode;
	}

	/**
	 * 获取验证码
	 * 
	 * @param tk
	 * @return
	 */
	public static String getVcode(String tk) {
		if (tk == null) {
			return null;
		}
		if (getVcode().get(tk) != null) {
			return (String) getVcode().get(tk).get("name");
		}
		return null;
	}

	/**
	 * 获取验证码map
	 * 
	 * @return
	 */
		public static Map<String, Map<String, Object>> getVcodeMap() {
			return getVcode();
		}
	public static void removeVcode(String tk) {
		if (getVcode().get(tk) != null) {
			vcode.remove(tk);
		}
	}

	public static Map<String, Map<String, Object>> getRandomStr() {
		if (randomStr == null) {
			randomStr = new HashMap<String, Map<String, Object>>();
		}
		return randomStr;
	}
	
	/**
	 * 检查并删除验证码
	 * 
	 * @param date
	 * @return
	 */
	public static boolean checkOutOfDateVcode(Date date) {
		
		try {
			List<String> lists=new ArrayList<String>();
			if (vcode != null) {
				for (Entry<String, Map<String, Object>> entry : vcode.entrySet()) {
					Long lg = 5 * 60 * 1000 - (LocalDateTime.getDateTime(date) - (Long) entry
							.getValue().get("creattime"));
					if (lg < 0) {
//					System.out.println("删除vcode，tk:" + entry.getKey()
//							+ ";value:" + entry.getValue());
					//	vcode.remove(entry.getKey());
						lists.add(entry.getKey());
					}

				}
				vcode.remove(lists);
			}

			return true;
		} catch (Exception e) {
			System.out.println("检测异常");
		}
		return one;
	}
	
	/**
	 * 检查并删除随机数
	 * 
	 * @param date
	 * @return
	 */
	public static boolean checkOutOfRandomStr(Date date) {
		if (randomStr != null) {
			for (Entry<String, Map<String, Object>> entry : randomStr.entrySet()) {
				Long lg = 5 * 60 * 1000 - (LocalDateTime.getDateTime(date) - (Long) entry
						.getValue().get("creattime"));
				if (lg < 0) {
				/*	System.out.println("删除随机数:" + entry.getKey()
							+ ";value:" + entry.getValue());*/
					randomStr.remove(entry.getKey());

				}

			}
		}

		return true;
	}
	
	/**
	 * 设置随机数缓存
	 */
	public static void setRandomStr(String str, String randow) {
		
		if (StringUtils.isNotBlank(str)) {
			if (randomStr == null) {
				randomStr = new HashMap<String, Map<String, Object>>();
			}
			if (randomStr.get(str) != null) {
				randomStr.remove(str);
			}
			Map<String, Object> object = new HashMap<String, Object>();
			object.put("name", str);
			object.put("creattime", System.currentTimeMillis());
			randomStr.put(str, object);
		}
		
		
	}

	/**
	 * 获取随机数缓存
	 */
	public static String getRandomStr(String str) {
		if (str == null) {
			return null;
		}
		if (getRandomStr().get(str) != null) {
			return (String) getRandomStr().get(str).get("name");
		}
		return null;
	}

	public static void updateOrderStatus(Date date) {

		if (getBusinOrder() != null) {
			if (one == false) {
				// 第一次运行查询数据库里的订单数据数据
				List<OrderIdAndDate> listvo = factory.czjorderService
						.getOrderList(OrderStatus.WAITFORRECEIVING.getIndex());

				for (OrderIdAndDate vo : listvo) {
					if (StringUtils.isNotBlank(vo.getId())
							&& vo.getCreatetime() != null) {
						AppMap.getBusinOrder().put(vo.getId(),
								LocalDateTime.getDateTime(vo.getCreatetime()));
					}
				}
				one = true;
			}

			List<OrderDateVO> orders = new ArrayList<OrderDateVO>();
			OrderDateVO vo=null;
			if (businOrder.keySet().size() != 0) {
				for (Entry<String, Long> entry : businOrder.entrySet()) {
					long deadline = 10*24*60*60*1000;// 10天 10*24*60*60*1000
					Long lg = (entry.getValue() + deadline)
							- LocalDateTime.getDateTime(date);
					if (lg < 0) {
						vo=new OrderDateVO();
						vo.setId(entry.getKey());
						vo.setUpdatetime(new Date());
						vo.setStatus(OrderStatus.FIN.getIndex());
						orders.add(vo);
					}

				}
			}
				
			if (orders != null && orders.size() > 0) {
				for (OrderDateVO o : orders) {
					factory.czjorderService.batchUpdateCzjOrder(o); // 批量更新订单状态
					businOrder.remove(o.getId());// 移除缓存
				}
			}

		}

	}

	public static void removeRandomStr(String str) {
		if (getRandomStr().get(str) != null) {
			randomStr.remove(str);
		}
	}

}
